//
//  LGSNodeViewController.m
//  LogicSim
//
//  Created by Jeremy Pereira on 03/03/2013.
//
//

#import "LGSNodeViewController.h"
#import "LGSModule.h"
#import "LGSDocument.h"
#import "LGSEdge.h"
#import "LGSName.h"
#import "LGSInputMappingDataSource.h"

@interface LGSNodeViewController()

@end

@implementation LGSNodeViewController
{
}

@synthesize displayedNode = displayedNode_;

-(void) setDisplayedNode: (LGSNode*) displayedNode
{
    displayedNode_ = displayedNode;
    if ([displayedNode isKindOfClass: [LGSModule class]])
    {
        [[self inputMappingDataSource] setModule: (LGSModule*)displayedNode];
    }
    else
    {
        [[self inputMappingDataSource] setModule: nil];
    }
    [[self inputTable] reloadData];
    [[self outputTable] reloadData];
}

-(IBAction) addInput:(id)sender
{
   	if ([[self displayedNode] canModifyConnections])
    {
        LGSModule* nodeAsModule = (LGSModule*) [self displayedNode];
        [nodeAsModule addInputNameString: NSLocalizedString(@"untitled", @"")];
        [[self document] updateNode: [self displayedNode]];
        [[self inputTable] reloadData];
        [[self inputMappingDataSource] reloadData];
    }
}

-(IBAction) addOutput:(id)sender
{
    if ([[self displayedNode] canModifyConnections])
    {
        LGSModule* nodeAsModule = (LGSModule*) [self displayedNode];
        [nodeAsModule addOutputNameString: NSLocalizedString(@"untitled", @"")];
        [[self document] updateNode: [self displayedNode]];
        [[self outputTable] reloadData];
    }
}

-(IBAction) removeInput:(id) sender
{
    NSInteger selectedRow = [[self inputTable] selectedRow];
    if (selectedRow != -1 && [[self displayedNode] canModifyConnections])
    {
        NSString* name = [[[self displayedNode] inputNames] objectAtIndex: selectedRow];
        [[self displayedNode] removeInputNameString: name];
        [[self document] updateNode: [self displayedNode]];
        [[self inputTable] reloadData];
        [[self inputMappingDataSource] reloadData];
    }
}

-(IBAction) removeOutput:(id) sender
{
    NSInteger selectedRow = [[self outputTable] selectedRow];
    if (selectedRow != -1 && [[self displayedNode] canModifyConnections])
    {
        NSString* name = [[[self displayedNode] outputNames] objectAtIndex: selectedRow];
        [[self displayedNode] removeOutputNameString: name];
        [[self document] updateNode: [self displayedNode]];
        [[self outputTable] reloadData];
    }
}



#pragma mark -
#pragma mark TableViewDataSource

-(NSInteger) numberOfRowsInTableView: (NSTableView*) tableView
{
    NSInteger ret = 0;
    if ([self displayedNode] != nil)
    {
        NSArray* names = nil;
        if (tableView == [self inputTable])
        {
            names = [[self displayedNode] inputNames];
        }
        else
        {
            names = [[self displayedNode] outputNames];
        }
        ret = [names count];
    }
    else
    {
        ret = 0;
    }
    return ret;
}

-(id) tableView: (NSTableView*) tableView
objectValueForTableColumn:(NSTableColumn*) tableColumn
            row:(NSInteger)row
{
    id ret = @"undefined";
    if ([self displayedNode] != nil)
    {
        NSArray* names = nil;
        if (tableView == [self inputTable])
        {
            names = [[self displayedNode] inputNames];
        }
        else
        {
            names = [[self displayedNode] outputNames];
        }
        ret = [names objectAtIndex: row];
        if ([[tableColumn identifier] isEqualToString: @"connectedFrom"])
        {
            LGSEdge* inputEdge = [[self displayedNode] input: ret];
            ret = [[[inputEdge input] name] nodePart];
        }
    }

    return ret;
}

-(void) tableView: (NSTableView*) tableView
   setObjectValue: (id) object
   forTableColumn: (NSTableColumn*) tableColumn
              row: (NSInteger)row
{
    if ([[self displayedNode] canModifyConnections])
    {
        NSArray* names = nil;
        if (tableView == [self inputTable])
        {
            names = [[self displayedNode] inputNames];
        }
        else
        {
            names = [[self displayedNode] outputNames];
        }
        NSString* oldName = [names objectAtIndex: row];
        bool updated = false;
        if (tableView == [self inputTable])
        {
            updated = [[self displayedNode] renameInputFromString: oldName
                                                         toString: object];
        }
        else
        {
            updated = [[self displayedNode] renameOutputFromString: oldName
                                                          toString: object];
        }
        if (updated)
        {
            [[self document] updateNode: [self displayedNode]];
        }
    }
}

@end
